﻿@using CuongHanh
@{
    ViewBag.Title = "Banner";
    Layout = "~/Views/Shared/_ALayout.cshtml";

}
@section script
{
    <script type="text/javascript">
        function search() {
            window.App.gvData.store.proxy.extraParams.query = window.App.txtSearch.getValue();
            window.App.gvData.store.load();
        }
        
        function getImage(value, record) {
            var branchId = '@ViewBag.BranchId';
            var url = '@Url.Content(AppUtil.THUMNAIL_BANNER_MAIN_PATH)';
            if (branchId == 1) url = '@Url.Content(AppUtil.THUMNAIL_BANNER_GEM_PATH)';
            else if (branchId == 2) url = '@Url.Content(AppUtil.THUMNAIL_BANNER_WOOD_PATH)';
            else if (branchId == 3) url = '@Url.Content(AppUtil.THUMNAIL_BANNER_STONE_PATH)';
           
              return "<img alt='no image' src='" + url + value + "'/>";
        }
        
        function handlerCommand(command, record) {

            if (command == 'View') {
                var width = parseFloat(record.data.Width);
                var height = parseFloat(record.data.Height);
                if (width > height) {
                    if (width > 600) {
                        height = (height / width) * 600;
                        width = 600;
                    }
                } else {
                    if (height > 500) {
                        width = (width / height) * 500;
                        height = 500;
                    }
                }
               
                var branchId = '@ViewBag.BranchId';
                var url = '@Url.Content(AppUtil.BANNER_MAIN_PATH)';
                if (branchId == 1)
                    url = '@Url.Content(AppUtil.BANNER_GEM_PATH)';
                else if (branchId == 2)
                    url = '@Url.Content(AppUtil.BANNER_WOOD_PATH)';
                else if (branchId == 3)
                    url = '@Url.Content(AppUtil.BANNER_STONE_PATH)';
                $('#wdImage').attr('src','');
                $('#wdImage').width(width);
                $('#wdImage').height(height);
                $('#wdImage').attr('src', url + record.data.Image);
                window.App.wdModify.setHeight(height);
                window.App.wdModify.setWidth(width);
                window.App.wdModify.center();
                window.App.wdModify.show();
            }
        }
    </script>
}
@(Html.X()
          .Viewport()
          .Layout(LayoutType.Border)
          .Items(view =>
                     {
                         Toolbar topMenu = new Toolbar
                                               {
                                                       Region = Region.North
                                               };
                         view.Add(topMenu);
                         topMenu.Add(Html.X()
                                             .Label()
                                             .Text("Tìm kiếm: "));
                         topMenu.Add(Html.X()
                                             .TextField()
                                             .ID("txtSearch")
                                             .Width(550)
                                             .EmptyText("Tìm kiếm theo: Tên"));
                         topMenu.Add(Html.X()
                                             .Button()
                                             .Icon(Icon.Find)
                                             .Text("Xem")
                                             .OnClientClick("search")
                                 );
                         topMenu.Add(new ToolbarFill());

                         topMenu.Add(Html.X()
                                             .Button()
                                             .Text("đồng bộ hình ảnh")
                                             .Icon(Icon.Add)
                                             .DirectEvents(de =>
                                             {
                                                 de.Click.Url = Url.Action("GetBannerFromFolder");
                                                 de.Click.ExtraParams.Add(new Parameter("branchId",
                                                                                        ViewBag.branchId + ""));
                                             })
                                 );
                         
                         topMenu.Add(Html.X()
                                             .Button()
                                             .Text("Thêm Banner")
                                             .Icon(Icon.Add)
                                             .DirectEvents(de =>
                                                               {
                                                               //    de.Click.Url = Url.Action("ShowAdd");
                                                                   de.Click.ExtraParams.Add(new Parameter("containerId",
                                                                                                          "wdModify"));
                                                                   de.Click.ExtraParams.Add(new Parameter("branchId",
                                                                                                          ViewBag.branchId + ""));
                                                               })
                                 );

                         view.Add(Html.X()
                                          .GridPanel()
                                          .ForceFit(true)
                                          .ID("gvData")
                                          .View(Html.X()
                                                        .GridView()
                                                        .EnableTextSelection(true)
                                          )
                                          .Region(Region.Center)
                                          .ColumnModel(column =>
                                                           {
                                                              
                                                               column.Add(Html.X()
                                                                                  .Column()
                                                                                  .DataIndex("Image")
                                                                                  .Text("Ảnh")
                                                                                  .Renderer("getImage")
                                                                       );
                                                               column.Add(Html.X()
                                                                                 .Column()
                                                                                 .DataIndex("Image")
                                                                                 .Text("Tên")
                                                                      );

                                                               column.Add(Html.X()
                                                                                .Column()
                                                                                .DataIndex("Width")
                                                                                .Text("Rộng")
                                                                     );
                                                               column.Add(Html.X()
                                                                                .Column()
                                                                                .DataIndex("Height")
                                                                                .Text("Cao")
                                                                     );
                                                               column.Add(Html.X()
                                                                                  .CommandColumn()
                                                                                  .Width(45)
                                                                                  .Commands(v => v.Add(new GridCommand
                                                                                  {
                                                                                      Icon = Icon.Image,
                                                                                      CommandName = "View",
                                                                                      Text = "Xem Ảnh"
                                                                                  })
                                                                                  )
                                                                                  .Listeners(c =>
                                                                                  {
                                                                                      c.Command.Handler = "handlerCommand(command, record);";
                                                                                  })
                                                                       );

                                                               column.Add(Html.X()
                                                                              .CommandColumn()
                                                                              .MinWidth(65)
                                                                              .MaxWidth(65)
                                                                              .Commands(v => v.Add(new GridCommand
                                                                              {
                                                                                  Icon = Icon.TableEdit,
                                                                                  CommandName = "ChangeBanner",
                                                                                  Text = "Đổi Banner"
                                                                              })
                                                                              )
                                                                              .DirectEvents(de =>
                                                                              {
                                                                                  de.Command.EventMask.ShowMask = true;
                                                                                  de.Command.ExtraParams.Add(new Parameter("containerId",
                                                                                                                           "wdModify"));
                                                                                  de.Command.ExtraParams.Add(new Parameter("command",
                                                                                                                           "command",
                                                                                                                           ParameterMode.Raw));
                                                                                  de.Command.ExtraParams.Add(new Parameter("id",
                                                                                                                           "record.data.Id",
                                                                                                                           ParameterMode.Raw));
                                                                                  de.Command.Url = Url.Action("Command");
                                                                                  de.AutoDataBind = true;
                                                                              })
                                                                   );
                                                               
                                                               column.Add(Html.X()
                                                                                  .CommandColumn()
                                                                                  .Width(120)
                                                                                  .PaddingSpec("0 10")
                                                                                  .Align(Alignment.Center)
                                                                                  .Commands(v =>
                                                                                            v.Add(new GridCommand
                                                                                                      {
                                                                                                              Icon = Icon.Delete,
                                                                                                              CommandName = "Delete",
                                                                                                              Text = "Xóa"
                                                                                                      })
                                                                                  )
                                                                                  .DirectEvents(de =>
                                                                                                    {
                                                                                                        de.Command.EventMask.ShowMask = true;
                                                                                                        de.Command.ExtraParams.Add(new Parameter("containerId",
                                                                                                                                                 "wdModify"));
                                                                                                        de.Command.ExtraParams.Add(new Parameter("command",
                                                                                                                                                 "command",
                                                                                                                                                 ParameterMode.Raw));
                                                                                                        de.Command.ExtraParams.Add(new Parameter("id",
                                                                                                                                                 "record.data.Id",
                                                                                                                                                 ParameterMode.Raw));
                                                                                                       // de.Command.Url = Url.Action("Command");
                                                                                                        de.Command.Confirmation.ConfirmRequest = true;
                                                                                                        de.Command.Confirmation.BeforeConfirm = "config.confirmation.title = 'Message'; config.confirmation.message = 'Do you want delete this record?';";
                                                                                                        de.AutoDataBind = true;
                                                                                                    })
                                                                       );
                                                           })
                                          .BottomBar(Html.X()
                                                             .PagingToolbar())
                                          .Store(Html.X()
                                                         .Store()
                                                         .RemotePaging(true)
                                                         .PageSize(30)
                                                         .ID("storeGvData")
                                                         .AutoLoad(true)
                                                         .AutoSync(false)
                                                         .Model(c => c.Add(Html.X()
                                                                                   .Model()
                                                                                   .Fields("Id",
                                                                                           "Image",
                                                                                           "Width",
                                                                                           "Height"
                                                                                   )
                                                                                   .IDProperty("Id")))
                                                         .Proxy(c => c.Add(Html.X()
                                                                                   .AjaxProxy()
                                                                                   .Json(true)
                                                                                   .Url(Url.Action("GetDatas"))
                                                                                   .ActionMethods(v => v.Read = HttpMethod.POST)
                                                                                   .Reader(Html.X()
                                                                                                   .JsonReader()
                                                                                                   .Root("data")
                                                                                                   .TotalProperty("total"))
                                                                                   .ExtraParams(param =>
                                                                                                    {
                                                                                                        param.Add(new Parameter("query",
                                                                                                                                "",
                                                                                                                                ParameterMode.Value));
                                                                                                        param.Add(new Parameter("branchId",
                                                                                                                                ViewBag.branchId + "",
                                                                                                                                ParameterMode.Value));
                                                                                                    }
                                                                                   )
                                                                             ))
                                                         .AutoLoadParams(c =>
                                                                             {
                                                                                 c.Add(new Parameter("start",
                                                                                                     "0",
                                                                                                     ParameterMode.Raw));
                                                                                 c.Add(new Parameter("limit",
                                                                                                     "30",
                                                                                                     ParameterMode.Raw));
                                                                             }))
                                 );
                     }))


@(Html.X()
          .Window()
          .ID("wdModify")
          .Hidden(true)
          .Modal(true)
          .Resizable(false)
          .Title("Hình ảnh")
          .Width(600)
          .Height(600)
          .Constrain(true)
          .StyleHtmlCls("background-color:white;")
          .Html("<img id='wdImage' alt='no image' style='width:600px;' src=''/>")
          )